using System;
using System.Collections.Specialized;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Configuration;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections;
using System.Data.SqlClient;

using Common;

public partial class tm_UC_Treeview : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            //javascript on check all
            TreeView1.Attributes.Add("onclick", "OnTreeClick(event)");
            
            //show tree on page load
            PopulateRootLevel();

            //show checked parent on page load
            TreeView treeView = TreeView1 as TreeView;
            TreeNodeCollection items = treeView.Nodes;
            CheckAll(items);

        }
    }
    protected void TreeView1_TreeNodePopulate(object sender, TreeNodeEventArgs e)
    {
        PopulateSubLevel(e.Node.Value, e.Node);

        //show checked child on page load
        TreeView treeView = TreeView1 as TreeView;
        TreeNodeCollection items = treeView.Nodes;
        CheckAll(items);

    }

    
    protected void PopulateNodes(DataTable dt, TreeNodeCollection nodes)
    {
        if (Session["arrayFld"] != null)
        {
            ArrayList arrayFld = (ArrayList)Session["arrayFld"];
            
            foreach (DataRow dr in dt.Rows)
            {
                TreeNode tn = new TreeNode();
                tn.Text = dr[arrayFld[0].ToString()].ToString().Trim();
                //tn.Value = dr["id"].ToString().Trim();
                tn.Value = dr[arrayFld[1].ToString()].ToString().Trim();
                nodes.Add(tn);

                //If node has child nodes, then enable on-demand populating
                tn.PopulateOnDemand = ((int)(dr["childnodecount"]) > 0);
            }
        }
    }
    protected void PopulateRootLevel()
    {
        if (Session["arrayFld"] != null)
        {
            ArrayList arrayFld = (ArrayList)Session["arrayFld"];

            string SQLCmd = "SELECT " + arrayFld[0].ToString() + ", " + arrayFld[1].ToString() + ",(select count(*) FROM " + arrayFld[2].ToString() + " WHERE ParentCode=sc." + arrayFld[1] + ") childnodecount FROM " + arrayFld[2].ToString() + " sc where ParentCode IS NULL";
            SqlConnection objConn = new SqlConnection(sysLibs.GetConnectionString);
            SqlCommand objCommand = new SqlCommand(SQLCmd, objConn);
            SqlDataAdapter da = new SqlDataAdapter(objCommand);
            DataTable dt = new DataTable();
            da.Fill(dt);

            PopulateNodes(dt, TreeView1.Nodes);
        }
    }

    protected void PopulateSubLevel(string ParentCode, TreeNode parentNode)
    {
        if (Session["arrayFld"] != null)
        {
            ArrayList arrayFld = (ArrayList)Session["arrayFld"];

            string SQLCmd = "SELECT " + arrayFld[0] + ", " + arrayFld[1] + ",(select count(*) FROM " + arrayFld[2] + " WHERE ParentCode=sc." + arrayFld[1] + ") childnodecount FROM " + arrayFld[2] + " sc where ParentCode=@ParentCode";
            SqlConnection objConn = new SqlConnection(sysLibs.GetConnectionString);
            SqlCommand objCommand = new SqlCommand(SQLCmd, objConn);
            objCommand.Parameters.Add("@ParentCode", SqlDbType.Char, 10).Value = ParentCode;
            SqlDataAdapter da = new SqlDataAdapter(objCommand);
            DataTable dt = new DataTable();
            da.Fill(dt);
            PopulateNodes(dt, parentNode.ChildNodes);
        }
    }
    //end show treeview

    protected void btnChon_Click(object sender, EventArgs e)
    {
        Hashtable tm_array = new Hashtable();
        ArrayList tm_arrayList = new ArrayList();

        if (TreeView1.CheckedNodes.Count > 0)
        {

            foreach (TreeNode item in TreeView1.CheckedNodes)
            {
                tm_array.Add(item.Value.ToString(), item.Text.ToString());
                tm_arrayList.Add(item.Value.ToString().Trim());
            }
        }
       

        //return array Hashtable
        Session["tm_ReturnArrayTV"] = tm_array;

        //return array List
        Session["tm_arrayListTV"] = tm_arrayList;
        
        //trong
        Session["tm_arrayList"] = Session["tm_arrayListTV"];

        //khoi
        Session["tm_ReturnArrayTV"] = tm_array;
    }

    protected void CheckAll(TreeNodeCollection items)
    {
        if (Session["tm_arrayListTV"] != null)
        {
            TreeView treeView = TreeView1 as TreeView;

            ArrayList al = (ArrayList)Session["tm_arrayListTV"];

            if ((treeView != null) && (items != null))
            {
                foreach (TreeNode item in items)
                {
                    for (int i = 0; i < al.Count; i++)
                    {
                        if (al[i].ToString().Trim() == item.Value.ToString().Trim())
                            item.Checked = true;
                        foreach (TreeNode temp in item.ChildNodes)
                        {
                            if (al[i].ToString().Trim() == temp.Value.ToString().Trim())
                            {
                                temp.Checked = true;
                            }
                            else CheckAll(temp.ChildNodes);

                        }
                    }
                }
            }
        }
    }
}
